/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.genitus.drum.api

import javax.ws.rs.core.{MediaType, Response}
import javax.ws.rs._

import ch.qos.logback.core.status.Status
import com.codahale.metrics.annotation.Timed
import com.google.inject.Inject
import org.genitus.drum.service.BuffService
import org.slf4j.LoggerFactory

/**
  * The rest api to obtain stat from buff.
  *
  * @author gwjiang (gwjiang@iflytek.com), 2016/11/21.
  */
@Path(PathConstants.statBasePath)
@Produces(Array(MediaType.APPLICATION_JSON))
@Consumes(Array(MediaType.APPLICATION_JSON))
class BuffResource @Inject() (
  val buffService: BuffService) {
  /** logger. */
  private[this] val log = LoggerFactory.getLogger(getClass.getName)

  /**
    * Get stat.
    *
    * @return stat.
    */
  @GET
  @Timed
  def getStat: Response = {
    log.info("get buff stat")

    try {
      Response.ok(buffService.getStat).build()
    } catch {
      case ex: Exception =>
        log.error("Exception while serving request", ex)
        throw new WebApplicationException(Status.ERROR)
    }
  }
}

/**
  * URL path constants.
  *
  * @author gwjiang (gwjiang@iflytek.com), 2016/8/25.
  */
object PathConstants {
  // -------------------------------- metrics --------------------------------
  /**
    * metrics base path.
    * /metrics is occupied by, so use /stat.
    * /stat
    */
  final val statBasePath = "/stat"
}
